package com.insight.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.Cache;import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

/**
 * T048: 列元数据实体类
 * 记录数据库表中列的元数据信息
 *
 * @author Insight Team
 */
@Entity
@Table(name = "metadata_column",
        indexes = {
                @Index(name = "idx_metadata_column_table", columnList = "table_id"),
                @Index(name = "idx_metadata_column_name", columnList = "column_name"),
                @Index(name = "idx_metadata_column_type", columnList = "data_type"),
                @Index(name = "idx_metadata_column_position", columnList = "column_position")
        })
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "metadata")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MetadataColumn {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 关联的表
     */
    @NotNull(message = "所属表不能为空")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "table_id", nullable = false)
    @JsonIgnoreProperties({"columns", "dataSource", "hibernateLazyInitializer", "handler"})
    private MetadataTable table;

    /**
     * 列名
     */
    @NotNull(message = "列名不能为空")
    @Size(max = 200, message = "列名长度不能超过200")
    @Column(name = "column_name", nullable = false, length = 200)
    private String columnName;

    /**
     * 数据类型 (VARCHAR, INT, DECIMAL等)
     */
    @NotNull(message = "数据类型不能为空")
    @Size(max = 100, message = "数据类型长度不能超过100")
    @Column(name = "data_type", nullable = false, length = 100)
    private String dataType;

    /**
     * 列长度 (对于VARCHAR等字符类型)
     */
    @Column(name = "max_length")
    private Integer columnLength;

    /**
     * 列精度 (对于DECIMAL等数值类型)
     */
    @Column(name = "numeric_precision")
    private Integer columnPrecision;

    /**
     * 列小数位数 (对于DECIMAL等数值类型)
     */
    @Column(name = "numeric_scale")
    private Integer columnScale;

    /**
     * 是否可空
     */
    @Column(name = "is_nullable")
    private Boolean nullable;

    /**
     * 默认值
     */
    @Column(name = "default_value", length = 500)
    private String defaultValue;

    /**
     * 列注释
     */
    @Column(name = "column_comment", length = 1000)
    private String columnComment;

    /**
     * 列在表中的位置(顺序)
     */
    @Column(name = "column_position")
    private Integer columnPosition;

    /**
     * 是否为主键
     */
    @Column(name = "is_primary_key")
    private Boolean isPrimaryKey;


    /**
     * 元数据记录创建时间
     */
    @CreationTimestamp
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    /**
     * 元数据记录更新时间
     */
    @UpdateTimestamp
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;

    /**
     * 生命周期回调: 持久化之前
     * 设置默认值,避免null
     */
    @PrePersist
    public void prePersist() {
        if (this.nullable == null) {
            this.nullable = true; // 默认可空
        }
        if (this.columnPosition == null) {
            this.columnPosition = 0; // 默认位置0
        }
        if (this.isPrimaryKey == null) {
            this.isPrimaryKey = false;
        }
    }

    /**
     * 业务方法: 获取完整列名 (schema.tableName.columnName)
     *
     * @return schema.tableName.columnName 或 tableName.columnName (如果schema为null)
     */
    public String getFullName() {
        if (table != null) {
            String tableFullName = table.getFullName();
            return tableFullName + "." + columnName;
        }
        return columnName;
    }
}
